<template>
    <el-dialog ref="dialogform" title="修改密码" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="密码" prop="password">
                <el-input class="w100p" show-password v-model="ruleForm.password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword">
                <el-input class="w100p" show-password v-model="ruleForm.confirmPassword" placeholder="请确认密码"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>

import webUser from "@/api/webUser";
export default {
    components: {},
    props: {
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        deviceTypeList: {
            type: Array,
            default: () => {
                return []
            }
        }
    },
    data () {
        var validatePassword = (rule, value, callback) => {
            if (!value || value.length < 6) {
                callback(new Error('请输入至少6位的密码'));
            } else {
                callback();
            }
        };
        var validateConfirmPassword = (rule, value, callback) => {
            if (!value) {
                callback(new Error('请再次输入密码'));
            } else if (this.ruleForm.password && this.ruleForm.password !== value) {
                callback(new Error('两次输入密码不一致'));
            } else {
                callback();
            }
        };
        return {
            addEditName: '添加',
            addEditApi: webUser.editPassword,
            ruleForm: {
                userId: ''
            },
            rules: {
                password: [
                    { required: true, validator: validatePassword, trigger: 'change' }
                ],
                confirmPassword: [
                    { required: true, validator: validateConfirmPassword, trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible (v) {
            v && (this.ruleForm.userId = this.data.userId)
        }
    },
    computed: {},
    mounted () { },
    methods: {
        //提交表单
        submitForm () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi(this.ruleForm).then(res => {
                        console.log(res)
                        this.cancel()
                        this.$message.success('修改成功')
                    }).catch(err => {
                        this.$message.warning('修改失败')
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm () {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel () {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose (done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.line {
    border-bottom: 1px solid #e6e6e6;
}

.icon {
    font-size: 20px;
    line-height: 29px;
    cursor: pointer;
}

::v-deep .noLabel {
    .el-form-item {
        .label {
            display: none;
        }

        .el-form-item__content {
            margin: 0;
        }
    }
}
</style>
  